package com.dhcc.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.dhcc.plugins.SqlPerformanceInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * MyBatis-Plus 配置类
 * @ProjectName Dhcc-Cloud
 * @PackageName com.dhcc.config
 * @Title MyBatisPlusConfig
 * @Date 2024/11/19 下午4:04
 * @Author LiuGuoting
 */
@Configuration
public class MyBatisPlusConfig {

    /**
     * 配置 MyBatis-Plus 插件
     *
     * @return MybatisPlusInterceptor
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        // 防止全表更新或删除
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        return interceptor;
    }

    /**
     * 注册自定义 SQL 执行日志打印与性能分析插件
     *
     * @return SqlPerformanceInterceptor
     */
    @Bean
    public Interceptor sqlPerformanceInterceptor() {
        // 设置阈值为 500 毫秒
        return new SqlPerformanceInterceptor();
    }
}
